
var layer = layui.layer //弹层
    ,table = layui.table //表格
    form = layui.form,$ = layui.jquery,laydate = layui.laydate,cbLists=[], id = getQueryVariable("id");
//主表数据数据
let dataList = [];
//明细数据
let detailList = [];
layer.ready(function () {
    //获取详情信息
    $.ajax({
        url:"receivePayment/refundAmount/getReceivePaymentInfo?id="+id,
        success:function(res){
            if(res.status == 200){
                //主表信息赋值
                if(null != res.data){
                    dataList =  res.data;
                    table.reload('mainstableid',{data : dataList});
                }
                return false;
            }else {
                layer.msg(res.msg, {icon: 5});
            }
        }
    });
    form.render();

});

table.render({
    elem: '#mains'
    ,data:dataList
    ,cols: [[
        {field:'billNo', title:'收款编号',sort:true,width: 220}
        ,{field:'time', title:'收款日期',sort:true,width: 120}
        ,{field:'customerName', title:'客户名称',sort:true,width: 120}
        ,{field:'currency', title:'币种',sort:true,width: 120}
        ,{field:'rmbRate', title:'人民币汇率',sort:true,width: 120}
        ,{field:'usdRate', title:'美金汇率',sort:true,width: 120}
        ,{field:'entryAmount', title:'入账金额',sort:true,width: 120}
        ,{field:'bankDeductionExcept', title:'银行扣费（外）',sort:true,width: 120}
        ,{field:'bankDeductionCapital', title:'银行扣费（本）',sort:true,width: 120}
        ,{field:'collectionAmount', title:'收汇金额',sort:true,width: 120}
        ,{field:'claimed', title:'已认领金额',sort:true,width: 120}
        ,{field:'splitCompletedAmount', title:'已拆分金额',sort:true,width: 120}
        ,{field:'undividedAmount', title:'未拆分金额',sort:true,width: 120}
        ,{field:'memo', title:'备注',sort:true,width: 120}
    ]]
    ,id: 'mainstableid'
    ,height: 300
    ,page: true
    ,done: function (res, curr, count) {
        //明细数据信息,默认展示第一条主表对应的明细数据
        if(dataList.length > 0 && res.data[0].detailList.length > 0){
            //默认显示第一条
            detailList = res.data[0].detailList;
        }
        table.render({
            elem: '#childinformation'
            ,data:detailList
            ,cols: [[
                {checkbox: true, fixed: true},
                {field: 'invoiceNumber',  title: '发票编号',sort:true},
                {field: 'contractNo',  title: '合同编号',sort:true},
                {field: 'refundAmount',  title: '退款金额',edit:'text'},
                {field: 'foreignBankDeductions',  title: '国外银行扣费'},
                {field: 'domesticBankCharge',  title: '国内银行扣费'},
                {field: 'totalContract',  title: '合同总额',sort:true},
                {field: 'totalInvoice',  title: '发票总额',sort:true},
                {field: 'claimCustomer',  title: '认领客户', sort:true},
                {field: 'claimSalesman',  title: '认领业务员', sort:true},
                {field: 'memo', title:'备注'}
            ]]
            ,id: 'childinformationtableid'
            ,page: true
            ,height: 300
        });
    }
});

//单击行事件
table.on('row(mainstable)', function (obj) {
    //获取当前选中的行id
    let id = obj.data.id;
    for(let i in dataList){
        if(id == dataList[i].id){
            table.reload('childinformationtableid', {data : dataList[i].detailList});
        }
    }
});

table.on('tool(childinformationtable)', function (obj) {
    if (obj.event === 'del') {
        layer.confirm('确认删除？',confirmInfo(3, 6), function (index) {
            //获取明细id
            let detailId = obj.data.detailId;
            //获取主表id
            let parentId = obj.data.parentId;
            let temp = dataList;
            for(let i in temp){
                if(parentId == temp[i].id){
                    for(let j in temp[i].detailList){
                        if(detailId == temp[i].detailList[j].detailId){
                            temp[i].detailList.splice(j, 1);
                            dataList = temp;
                            layer.msg("删除成功", {icon: 6});
                            table.reload('childinformationtableid', {data : temp[i].detailList});
                            layer.close(index);
                            break;
                        }
                    }
                }
            }
        });
    }
});

table.on('edit(childinformationtable)',function (obj) {
    let old=$(this).prev().text();//旧值
    //判断数据类型 ,,,,,,,
    if((obj.field == "refundAmount")&&!isNumber(obj.value)) {
        layer.msg('请输入数字');
        $(this).val(old);
        return;
    }
    if(obj.field == "refundAmount"){
        //退款金额不能大于已认领金额
        let flag = checkRefundAmount(obj);
        if(!flag){
            layer.msg('退款金额不能大于已认领金额');
            $(this).val(old);
            return;
        }
        //父id
        let parentId = obj.data.parentId;
        for(let i in dataList){
            if(dataList[i].id === parentId){
                dataList[i].detailList[0].refundAmount = obj.value;
                table.reload('childinformationtableid', {data : dataList[i].detailList});
                break;
            }
        }
    }
});

function checkRefundAmount(obj){
    let flag = true;
    //父id
    let parentId = obj.data.parentId;
    for(let i in dataList){
        if(dataList[i].id === parentId){
            let claimed = dataList[i].claimed;
            let res = new BigNumber(obj.value).comparedTo(new BigNumber(claimed));
            if(res === 1){
                flag = false;
                break;
            }
        }
    }
    return flag;
}

$("#choose").on("click",function () {
    //向后台组装数据
    let reqList = dataList;
    let datas = JSON.stringify(reqList);
    $.ajax({
        type:"POST",
        dataType:"json",
        contentType: 'application/json',
        data:datas,
        url:"receivePayment/refundAmount/addRefundAmount",
        success:function(res){
            if(res.status == 200){
                layer.msg("保存成功", {icon: 6});
                setTimeout(function () {
                    window.parent.location.reload();
                    //保存成功则关闭此页面
                    let index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                },1000)
            }else {
                layer.msg(res.msg, {icon: 5});
            }
            return false;
        }
    });
})
$("#shutdown").on("click",function () {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
});

function isNumber(val) {
    var regPos = /^\d+(\.\d+)?$/; //非负浮点数
    var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
    if (regPos.test(val) || regNeg.test(val)) {
        return true;
    } else {
        return false;
    }
}